// 登录接口
const express = require("express");

const router = express.Router();

const Profile = require("../../model/profile");
const keys = require("../../config/key").secretOrkey;
const passport = require("passport");

/**
 * @$route GET api/profile/test
 * @desc 返回数据
 * @access public
 */
router.get("/test", (req, res) => {
	res.json({
		msg: "profile work",
	});
});
/**
 * @$route POST api/profile/add
 * @desc 创建信息接口
 * @access privite
 */
router.post(
	"/add",
	passport.authenticate("jwt", { session: false }),
	(req, res) => {
		const profileFields = {};
		if (req.body.type) profileFields.type = req.body.type;
		if (req.body.describe) profileFields.describe = req.body.describe;
		if (req.body.income) profileFields.income = req.body.income;
		if (req.body.expend) profileFields.expend = req.body.expend;
		if (req.body.cash) profileFields.cash = req.body.cash;
		if (req.body.remark) profileFields.remark = req.body.remark;
		if (req.body.date) profileFields.date = req.body.date;
		new Profile(profileFields)
			.save()
			.then((profile) => res.json(profile))
			.catch((err) => console.log(err));
	}
);

/**
 * @$route get api/profile
 * @desc 请求所有信息
 * @access privite
 */
router.get(
	"/",
	passport.authenticate("jwt", { session: false }),
	(req, res) => {
		Profile.find()
			.then((profile) => {
				if (!profile) {
					return res.status(404).json({ msg: "没有任何内容" });
				}
				res.json(profile);
			})
			.catch((err) => {
				return res.status(404).json({ msg: err });
			});
	}
);

/**
 * @$route get api/profile/:id
 * @desc 请求单个信息
 * @access privite
 */
router.get(
	"/:id",
	passport.authenticate("jwt", { session: false }),
	(req, res) => {
		Profile.findOne({ _id: req.params.id })
			.then((profile) => {
				if (!profile) {
					return res.status(404).json({ msg: "没有任何内容" });
				}
				res.json(profile);
			})
			.catch((err) => {
				return res.status(404).json({ err, msg: "没有任何内容" });
			});
	}
);

/**
 * @$route post api/profile/edit/:id
 * @desc 请求单个信息
 * @access privite
 */
router.post(
	"/edit/:id",
	passport.authenticate("jwt", { session: false }),
	(req, res) => {
		const profileFields = {};
		if (req.body.type) profileFields.type = req.body.type;
		if (req.body.describe) profileFields.describe = req.body.describe;
		if (req.body.income) profileFields.income = req.body.income;
		if (req.body.expend) profileFields.expend = req.body.expend;
		if (req.body.cash) profileFields.cash = req.body.cash;
		if (req.body.remark) profileFields.remark = req.body.remark;
		if (req.body.date) profileFields.date = req.body.date;
		Profile.findOneAndUpdate(
			{
				_id: req.params.id,
			},
			{ $set: profileFields },
			{
				new: true,
			}
		)
			.then((profile) => res.json(profile))
			.catch((err) => console.log(err));
	}
);

/**
 * @$route delete api/profile/delete/:id
 * @desc 请求单个信息
 * @access privite
 */
router.delete(
	"/delete/:id",
	passport.authenticate("jwt", { session: false }),
	(req, res) => {
		Profile.findOneAndRemove({
			_id: req.params.id,
		})
			.then((data) => {
				console.log("x1", "data", data);
				if (!data) {
					return res.status(404).json({ err, msg: "删除失败!" });
				}
				return res.json({ data, msg: "删除成功!" });
			})
			.catch((err) => {
				return res.status(404).json({ err, msg: "删除失败!" });
			});
	}
);

module.exports = router;
